package com.example.xlmkotlinwanandroid.ext

import android.app.Activity
import android.content.res.Resources
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import com.example.xlmkotlinwanandroid.util.ConstantUtil

//封装toast拓展函数
fun Activity.toast(message: String) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}

//打印出activityName
fun Activity.getActivityName() {
    //利用常量类做封装，避免硬编码
    Log.d(ConstantUtil.ACTIVITY_NAME, this.javaClass.simpleName)
}

fun Float.dp2Px(): Int {
    val scale: Float = Resources.getSystem().displayMetrics.density
    return (this * scale + 0.5f).toInt()
}


fun <T> LifecycleOwner.observer(liveData: LiveData<T>, action: (t: T) -> Unit) {
    liveData.observe(this) { t -> action(t) }
}